// 上报模块
/*----------依赖模块--------- */
const express = require('express');
const router = express.Router();
const db = require('../db/db');
const back = require('../utils/back');
const md5 = require('md5');
const middle = require('../middle/auth');
const dayjs = require('dayjs');
const token = require('../controller/token')

/*----------API方法---------- */

const shop = {}

//商品列表
shop.list = async (req, res) => {
    const params = req.query;
    // 如sql错
    const hasMobile = await db.query('select * from coupons');
    if (hasMobile === false) {
        res.send(back.ERRTYPE.ERROR_DB, '查询失败');
        return;
    }
    res.send(back.Data(hasMobile));
    return;
}

shop.info = async (req, res) => {
    const params = req.query;
    if (!params.id) {
        res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, "参数缺少"))
        return;
    }
    const shopInfo = await db.query('select * FROM coupons where id=?', [params.id]);
    if (shopInfo === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }
    if (shopInfo < 1) {
        res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, '没有这类道具'));
        return;
    }
    res.send(back.Data(shopInfo[0]));
    return;
}

shop.buy = async (req, res) => {
    const params = req.body;
    const { user_id } = req.userInfo;
    if (!params.id || !params.count) {
        res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, "缺少参数"))
        return;
    }
    //查找商品
    const buyInfo = await db.query("select * from coupons where id=?", [params.id]);
    if (buyInfo === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }
    if (buyInfo < 1) {
        res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, '没有这类商品'));
        return;
    }
    //价格计算
    var userInfo = await db.query('select * from user where user_id=?', [user_id])
    if (userInfo === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }
    const total = buyInfo[0].price * params.count;
    if (total > userInfo[0].integral) {
        res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, "积分不足"))
        return;
    }

    const updateIntegral = await db.query('update user set integral=integral-? where user_id=?', [(buyInfo[0].price * params.count), user_id])
    if (updateIntegral === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '购买失败'));
        return;
    }

    //购买后背包处理
    const bagGood = await db.query('select * from mycoupons where shopid=? and userid=?', [params.id, user_id])
    if (bagGood === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查找失败'));
        return;
    }
    let bagData = {};
    if (bagGood.length < 1) {
        bagData = await db.query('INSERT INTO mycoupons (userid,shopid,product_name,num,product_description,price,product_image) VALUES (?, ?, ?, ?, ?, ?, ?)',
            [
                user_id,
                params.id,
                buyInfo[0].product_name,
                params.count,
                buyInfo[0].product_description,
                buyInfo[0].price,
                buyInfo[0].product_image]);
        if (bagData === false) {
            res.send(back.ERR(back.ERRTYPE.ERROR_DB, '数据库插入失败'));
            return;
        }

    } else {
        bagData = await db.query('update mycoupons set num=num+? where shopid=? and userid=?',
            [params.count, params.id, user_id]);
        if (bagData === false) {
            res.send(back.ERR(back.ERRTYPE.ERROR_DB, '数据库插入失败'));
            return;
        }
    }

    //积分更新
    var updateuserInfo = await db.query('select * from user where user_id=?', [user_id])
    if (updateuserInfo === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }

    res.send(back.List(params.count, updateuserInfo[0]))
    return;
}

/*---------路由---------- */
// 注册
router.get('/list', shop.list)
router.get('/info', shop.info)
router.post('/buy', middle.userAuth, shop.buy)


/*-------暴露----------- */
module.exports = router;